package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import com.itheima.constant.MenuConstant;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Menu;
import com.itheima.service.MenuService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/menu")
public class MenuController {
    @Reference
    MenuService menuService;

    @GetMapping("/getAllMenuBaseUserNameWithChildren")
    public Object getAllMenuBaseUserNameWithChildren() {
        String username = SecurityContextHolder.getContext().getAuthentication().getName();
        List<Menu> allBaseUserName = menuService.findAllBaseUserNameWithChildren(username);
        return allBaseUserName;
    }

    @GetMapping("getMenuById")
    public Menu getMenuById(int id) {
        Menu menu = menuService.selectMenuByIdWithoutChildren(id);
        return menu;
    }


    //普通分页查询
    @PostMapping("/findAllWithoutChildrenByPage")
    public Object findAllWithoutChildrenByPage(@RequestBody QueryPageBean queryPageBean) {
        PageInfo<Menu> allWithoutChildrenByPage = menuService.findAllWithoutChildrenByPage(queryPageBean);
        return allWithoutChildrenByPage;
    }

    @GetMapping("/findMenuByLevel")
    public Object findMenuByLevel(int level) {
        List<Menu> menuByLevel = menuService.findMenuByLevel(level);
        return menuByLevel;
    }

    @GetMapping("/getSonMenu")
    public Object getSonMenu(Integer menuId, String queryString) {
        List<Menu> menus = null;
        menus = menuService.queryChildren(menuId);
        return menus;
    }

    @PreAuthorize("hasRole('ROLE_ADMIN')")
    @GetMapping("/getAllMenu")
    public Object getAllMenu(QueryPageBean queryPageBean) {
        PageInfo<Menu> menuPageInfo = menuService.findAllWithoutChildrenByPage(queryPageBean);
        return menuPageInfo;
    }


    @PreAuthorize("hasRole('ROLE_ADMIN')")
    @PostMapping("/create")
    public Object create(@RequestBody Menu menu) {
        MenuConstant menuConstant = new MenuConstant();
        try {
            menuService.createMenu(menu);
            menuConstant.setStatus("200");
            menuConstant.setMsg("OK");
        } catch (Exception e) {
            e.printStackTrace();
            menuConstant.setStatus("520");
            menuConstant.setMsg("Failed");
        }
        return menuConstant;
    }

    @PreAuthorize("hasRole('ROLE_ADMIN')")
    @PostMapping("/update")
    public Object update(@RequestBody Menu menu) {
        menuService.updateMenu(menu);
        MenuConstant menuConstant = new MenuConstant();
        menuConstant.setStatus("200");
        menuConstant.setMsg("OK");
        return menuConstant;
    }

    @PreAuthorize("hasRole('ROLE_ADMIN')")
    @GetMapping("/delete")
    public Object delete(Integer id) {
        MenuConstant menuConstant = new MenuConstant();
        try {
            menuService.deleteByMenuIdSafe(id);
            menuConstant.setStatus("200");
            menuConstant.setMsg("OK");
        } catch (Exception e) {
            e.printStackTrace();
            menuConstant.setStatus("520");
            menuConstant.setMsg("FAILED");
        }
        return menuConstant;
    }


    //条件分页查询
    @PostMapping("/queryMenuWithCondition")
    public Object queryMenuWithCondition(@RequestBody QueryPageBean queryPageBean) {
        PageInfo pageInfo = menuService.queryByKeyWord(queryPageBean);
        return pageInfo;
    }
}
